﻿namespace Jsl.FxCop
{
    using System.Linq;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    [TestClass]
    public class TestEventHelper
    {
        [TestMethod]
        public void Normal_event_adds_are_found()
        {
            var events = EventHelper.EventsUsedInMethod(
                typeof(Targets.EventHelperTarget)
                    .GetMethod(method => method.Name.Name == "SubscribesToNormalEvent"));

            AssertEx.That(() => events.Count() == 1);
            EventInformation eventInfo = events[0];
            AssertEx.That(() => eventInfo.EventMember.Name.Name == "Click");
            AssertEx.That(() => eventInfo.Handler.Name.Name == "SubscribesToNormalEvent_Handler");
            AssertEx.That(() => eventInfo.IsEventAdder);
        }

        [TestMethod]
        public void Normal_event_removes_are_found()
        {
            var events = EventHelper.EventsUsedInMethod(
                typeof(Targets.EventHelperTarget)
                    .GetMethod(method => method.Name.Name == "UnsubscribesFromNormalEvent"));

            AssertEx.That(() => events.Count() == 1);
            EventInformation eventInfo = events[0];
            AssertEx.That(() => eventInfo.EventMember.Name.Name == "Click");
            AssertEx.That(() => eventInfo.Handler.Name.Name == "UnsubscribesFromNormalEvent_Handler");
            AssertEx.That(() => !eventInfo.IsEventAdder);
        }

        [TestMethod]
        public void External_event_adds_are_found()
        {
            var events = EventHelper.EventsUsedInMethod(
                typeof(Targets.EventHelperTarget)
                    .GetMethod(method => method.Name.Name == "SubscribesToExternalEvent"));

            AssertEx.That(() => events.Count() == 1);
            EventInformation eventInfo = events[0];
            AssertEx.That(() => eventInfo.EventMember.Name.Name == "Click");
            AssertEx.That(() => eventInfo.Handler.Name.Name == "SubscribesToExternalEvent_Handler");
            AssertEx.That(() => eventInfo.IsEventAdder);
        }

        [TestMethod]
        public void External_event_removes_are_found()
        {
            var events = EventHelper.EventsUsedInMethod(
                typeof(Targets.EventHelperTarget)
                    .GetMethod(method => method.Name.Name == "UnsubscribesFromExternalEvent"));

            AssertEx.That(() => events.Count() == 1);
            EventInformation eventInfo = events[0];
            AssertEx.That(() => eventInfo.EventMember.Name.Name == "Click");
            AssertEx.That(() => eventInfo.Handler.Name.Name == "UnsubscribesFromExternalEvent_Handler");
            AssertEx.That(() => !eventInfo.IsEventAdder);
        }
    }
}